package com.htyz.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.htyz.entity.Response;
import com.htyz.entity.Review;
import com.htyz.entity.Start;
import com.htyz.entity.User;
import com.htyz.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/aa")
public class ChartsController {
    @Autowired
    private ChartsMapper chartsMapper;
    @Autowired
    private CityChartMapper cityChartMapper;
    @Autowired
    private PaiHangMapper paiHangMapper;
    @Autowired
    private ScenicSpotsMapper scenicSpotsMapper;
    @Autowired
    private UserMapper userMapper;
    Random random = new Random();
    @Autowired
    private StartMapper startMapper;
    @Autowired
    private ReviewMapper reviewMapper;


    @GetMapping("/list")
    public String getList(){
        return JSON.toJSONString(chartsMapper.selectList(null));
    }

    @GetMapping("/getCityList")
    public String getCityList(){
        return JSON.toJSONString(cityChartMapper.selectList(null));
    }

    @GetMapping("/getPaiHangList")
    public String getPaiHangList(){
        return JSON.toJSONString(paiHangMapper.selectList(null));
    }

    @GetMapping("/getsSList")
    public String getsSList(){
        return JSON.toJSONString(scenicSpotsMapper.selectList(null));
    }

    @PostMapping("/login")
    public String login( @RequestBody  User user){
        String username = user.getAccNumber();
        String password = user.getPassword();
        System.out.println(username+password);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("acc_number", username);
        User user1=userMapper.selectOne(queryWrapper);
        if(user1==null){
            return JSON.toJSONString(new Response(-1,"用户未注册",new User()));
        }
        if(!user1.getPassword().equals(password)){
            return JSON.toJSONString(new Response(-1,"密码错误",new User()));
        }
        return JSON.toJSONString(new Response(200,"登录成功",user1));
    }

    @PostMapping("/register")
    public String register(@RequestBody  User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("acc_number", user.getAccNumber());
        List<User> existingUsers = userMapper.selectList(queryWrapper);
        User user1 = new User();
        if(existingUsers.size()>0){
            return JSON.toJSONString(new Response(-1,"用户名已存在",new User()));
        }
        user1.setAccNumber(user.getAccNumber());
        user1.setPassword(user.getPassword());
        user1.setCount(random.nextInt(50)+1);
        user1.setName(user.getName());
        userMapper.insert(user1);
        return JSON.toJSONString(new Response(200,"注册成功",user1));
    }


    //查看评论
    @GetMapping("/getReview")
    public String getReview(){
        return JSON.toJSONString(reviewMapper.selectList(null));
    }

    //查看评论
    @PostMapping("/getReviewByUserId")
    public String getReviewByUserId(@RequestBody Review review){
        QueryWrapper<Review> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("user_id", review.getUserId());
        List<Review> reviews = reviewMapper.selectList(QueryWrapper);
        return JSON.toJSONString(new Response(200,"查询成功",reviews));
    }

    //查看评论
    @PostMapping("/getReviewById")
    public String getReviewById(@RequestBody Review review){
        QueryWrapper<Review> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("scenic_id", review.getScenicId());
        List<Review> reviews = reviewMapper.selectList(QueryWrapper);
        return JSON.toJSONString(new Response(200,"查询成功",reviews));
    }

    //写评论
    @PostMapping("/addReview")
    public String addReview(@RequestBody Review review){
        Review review1 = new Review();
        review1.setCount(review.getCount());
        review1.setCreateTime(new Date());
        review1.setUserId(review.getUserId());
        review1.setUserName(review.getUserName());
        review1.setScenicId(review.getScenicId());
        review1.setScenicName(review.getScenicName());
        reviewMapper.insert(review1);
        return JSON.toJSONString(new Response(200,"评论成功",review1));
    }

    //查看收藏
    @PostMapping("/getStartByUserId")
    public String getStart(@RequestBody Start start){
        QueryWrapper<Start> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",start.getUserId());
        queryWrapper.eq("status",1);
        List<Start> starts = startMapper.selectList(queryWrapper);
        return JSON.toJSONString(new Response(200,"查询成功",starts));
    }

    //查看评论
    @PostMapping("/getStartById")
    public String getStartById(@RequestBody Start start){
        QueryWrapper<Start> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("scenic_id", start.getScenicId());
        QueryWrapper.eq("user_id", start.getUserId());
        List<Start> Starts = startMapper.selectList(QueryWrapper);
        return JSON.toJSONString(new Response(200,"查询成功", Starts));
    }


    @PostMapping("/addStart")
    public String addStart(@RequestBody Start start){
        //查询用户他的状态，是否有收藏该攻略，如果有就修改没有就添加
        QueryWrapper<Start> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("scenic_id", start.getScenicId());
        QueryWrapper.eq("user_id", start.getUserId());
        Start start1 = startMapper.selectOne(QueryWrapper);
        Start start2 = new Start();
        start2.setScenicId(start.getScenicId());
        start2.setScenicName(start.getScenicName());
        start2.setStatus(start.getStatus());
        start2.setUserName(start.getUserName());
        start2.setCreateTime(new Date());
        start2.setUserId(start.getUserId());
        //添加
        if(start1==null){
            startMapper.insert(start2);
            return JSON.toJSONString(new Response(200,"收藏成功", start2));
        }
        //更新
        startMapper.update(start2,QueryWrapper);
        if(start.getStatus()==0) return JSON.toJSONString(new Response(200,"取消收藏", start2));
        return JSON.toJSONString(new Response(200,"收藏成功", start2));
    }
}
